import { getSessionItem, setSessionItem } from "@/basa/utils";
import {
  orderList
} from "../api/template-request";
// import {
//   listPageOrder,
//   removeOrder,
//   enteringRevokeApproval
// } from "../api/orderEntry";
// import { formatNumber } from "@/basa/utils";

export default {
  data() {
    return {
      workItemList:[
        {
          dicKey:'0',
          dicValue:'待审批'
        },
        {
          dicKey:'1',
          dicValue:'已审批'
        }
      ],
      businessBillList:[
        {
          dicKey:'S0202',
          dicValue:'订单关闭'
        },
        {
          dicKey:'S0201',
          dicValue:'订单录入'
        }
      ],
      approveStatusBillList:[
        // {
        //   dicKey:'0',
        //   dicValue:'待提交'
        // },
        // {
        //   dicKey:'1',
        //   dicValue:'审批中'
        // },
        // {
        //   dicKey:'2',
        //   dicValue:'已撤回'
        // },
        {
          dicKey:2,
          dicValue:'审批通过'
        },
        {
          dicKey:3,
          dicValue:'审批退回'
        },
        {
          dicKey:4,
          dicValue:'审批不通过'
        },
        {
          dicKey:5,
          dicValue:'协办'
        },
        {
          dicKey:6,
          dicValue:'转审'
        }
      ],
      // 高级搜索相关
      isShowSearch: true,
      formData: {
        orderNo:"",
        orderName:"",
        thirdCategoriesOfName:"",
        partyBName:"",
        reportInOrgName:"",
        createTime:"",
        status:"",
        applyTime:[],
        orderDeliveryTimeStart:"",
        orderDeliveryTimeEnd:"",
        // bizStatus: "",
        pageNo: 1,
        pageSize: 10,
        approveStatus:""
      },
      // Tab切换
      isActive: 0,
      focuses: [
        { focus: "item-list" },
        { focus: "approved" },
        { focus: "pending-approval" }
      ],
      tableData: [],
      tableColumnData: [
        {
          label: "序号",
          type: "index",
          fixed: true
        },
        {
          label: "订单编号",
          prop: "businessData.orderNo",
          width: "150",
          fixed: true
        },
        {
          label: "审批类型",
          prop: "todoData.businessBillTypeName",
          width: "150",
        },
        {
          label: "订单名称",
          prop: "businessData.orderName",
          width: "300"
        },
        {
          label: "分类名称",
          prop: "businessData.thirdCategoriesOfName",
          width: "100"
        },
        {
          label: "供应商",
          prop: "businessData.partyBName",
          width: "200"
        },
        {
          label: "填报单位",
          prop: "businessData.reportInOrgName",
          width: "200"
        },
        {
          label: "编制人",
          prop: "businessData.compileUser",
          width: "80"
        },
        {
          label: "订单发出时间",
          prop: "businessData.orderDeliveryTime",
          width: 100
        },
        {
          label: "审批状态",
          prop: "todoData.workItemStatusString",
          width: 70
        },
        // {
        //   label: "审批结果",
        //   prop: "businessData.approveStatus",
        //   width: 70,
        //   formatterName:"approveStatus"
        // }
      ],
      // 表格数据处理
      formatterList: {
        approveStatus(row){
          console.log('row>>>>',row)
          switch(row.businessData.approveStatus){
            // case "0":
            //   return '待提交';
            // case "1":
            //   return '审批中';
            // case "2":
            //   return '已撤回';
            case "3":
              return '审批通过';
            case "4":
              return '审批退回';
            // case "5":
            //   return '审批不通过';
            
          }
        },
      },
      // 分页相关
      isShowPagination: false,
      totalNum: 0,
      pageNoCode: this.getLocalPageNo(),
      pageSizeCode: this.getLocalPageSize(),
      tableName: this.tableList.orderApproval,
      bizStatusType: {
        0: "待审批",
        1: "已审批",
        4: "已撤回",
        5: "审批通过",
        6: "审批退回"
      },
      buttonBool: false,
      formDataSearch: "",
      localSearchData: {},
    };
  },
  // inject: ["reload"],
  methods: {
    search(searchData) {
      let type = ''
      switch(this.isActive){
        case 0:
          type = 'all';
          break;
        case 1:
          type = 'pending';
          break;
        case 2:
          type = 'approved';
          break;

      }
      // return type;
     
      this.pageNoCode = 1;
      this.formDataSearch = searchData;
      this.localSearchData = JSON.parse(JSON.stringify(searchData));
      setSessionItem("pageNo", this.tableName, {
        pageNo: this.pageNoCode,
        searchForm: this.localSearchData
      });
      this.getList(this.formDataSearch,type);
    },
    handleTrim(key) {
        this.$set(
          this.formData,
          key,
          this.formData[key].replace(/(^\s*)|(\s*)$/g, "")
        );
      },
    // 列表数据获取
    getList(reqObj,type) {
      console.log('reqObj',reqObj)
      console.log('type',type)
      // if(type == 'approved'){
      //   this.tableColumnData.push(
      //     {
      //       label: "审批结果",
      //       prop: "businessData.approveStatus",
      //       width: 70,
      //       formatterName:"approveStatus"
      //     }
      //   )
      // }
      let obj = {};
      obj = typeof reqObj == "object" ? reqObj : obj;
      obj.pageNo = this.pageNoCode;
      obj.pageSize = this.pageSizeCode;
      obj.isShowPageLoading = true;
      // this.formData.pageSize = this.pageSizeCode;
      // this.formData.pageNo = this.pageNoCode;
      obj.businessBillTypes = this.formData.businessBillTypes?[this.formData.businessBillTypes]:[];
      obj.operResultTypes = this.formData.approveStatus == 3?[3,9]:!this.formData.approveStatus?[]:[this.formData.approveStatus]
      // let localData = getSessionItem(
      //   "pageNo",
      //   this.tableList.orderApproval
      // );
      // let data = localData && localData.searchForm;
      //   let obj = {
      //     isShowPageLoading: true,
      //     loadingMessage: "加载中,请稍等..."
      //   };
        orderList(obj,type).then(res => {
          this.isShowPagination = res.data.data.recordsTotal > 0;
          this.tableData = res.data.data.rows
          console.log("数量保留三位", this.tableData);
          this.tableData.map(e => {
            e.businessData.orderDeliveryTime = e.businessData.orderDeliveryTime
              ? e.businessData.orderDeliveryTime.substr(0, 10)
              : "";
          });
          this.tableData.map(e => {
            e.todoData.workItemStatus1 = this.bizStatusType[e.todoData.workItemStatus];
            // e.orderQuantity = formatNumber(e.orderQuantity, 3);
          });
          this.totalNum = res.data.data.recordsTotal;
          this.pageNoCode = res.data.data.pageNo;

        });
    },
    searchSelect(){

    },
    searchSelect1(){

    },
    handleTime(value){
      console.log(value)
      this.formData.orderDeliveryTimeStart = value?value[0]:""
      this.formData.orderDeliveryTimeEnd = value?value[1]:""
    },
    // 提示弹窗
    deleteMessageBox(obj, params, status, callBack) {
      this.$confirm(obj.content, obj.title, {
        distinguishCancelAndClose: true,
        confirmButtonText: obj.confirmButtonText,
        cancelButtonText: "取消",
        customClass: "ccui-message-box-custom"
      })
        .then(() => {
          callBack(params, status);
        })
        .catch(e => {
          console.log(e);
        });
    },
    // 删除
    // removeOrder(params, status) {
    //   removeOrder(params).then(res => {
    //     this.callBackThenMessage(res, status);
    //   });
    // },
    // 撤回
    // enteringRevokeApproval(params, status) {
    //   enteringRevokeApproval(params).then(res => {
    //     this.callBackThenMessage(res, status);
    //   });
    // },
    // 删除 / 撤回成功提示message
    callBackThenMessage(res, status) {
      let { code, message } = res.data;
      if (code === "0"){
        this.$message.success(message);
      }else {
        this.$showErrorMessage(message)
      }
      // this.$message({
      //   message: message,
      //   type: code === "0" ? "success" : "error",
      //   duration: 2000,
      //   ccuiCustom: true
      // });
      this.getList(status);
    },
    // 每页条数发生改变
    handleSizeChange(val,id) {
      this.pageNoCode = 1;
      if (val !== "none") {
        this.pageSizeCode = val;
        setSessionItem("pageSize", this.tableName, val);
      }
      this.getList(this.formDataSearch,id);
    },
    // 当前页发生改变
    handleCurrentChange(val,id) {
      let searchFormData = getSessionItem("pageNo", this.tableName) || {};
      setSessionItem("pageNo", this.tableName, {
        pageNo: val,
        searchForm: searchFormData.searchForm || {}
      });
      this.formDataSearch = {
        ...this.formDataSearch,
        ...searchFormData.searchForm
      };
      // 跳转翻页都会调这个方法
      this.pageNoCode = val;
      console.log('this.pageNoCode',this.pageNoCode)
      this.getList(this.formDataSearch,id);
    },
    // 跳转详情
    handleGoDetail(r) {
      this.$router.push("/orderManagement/entry/detail/" + r.id);
    },
    // 跳转编辑
    handleGoEdit(r) {
      // this.$router.push("/orderManagement/approval/approval-operation" + r.id);
      const {businessData,todoData} = r
      this.$store.dispatch("setBusinessList", todoData);
      const businessList = JSON.stringify(todoData);
      sessionStorage.setItem("businessList", businessList)
      sessionStorage.setItem("businessData", JSON.stringify(businessData))
      console.log(r)
      this.$router.push({
        path:`/orderManagement/approval/approval-operation/${businessData.id}`,
        query:{
          businessBillTypeCode:todoData.businessBillTypeCode
        }
      })
        
    },
    // 跳转新增
    handleGoNew() {
      this.$router.push("/orderManagement/entry/add");
    },
    // Tab切换
    tabChange(data) {
      console.log(data, "Tab切换");
      this.$router.push(`/orderManagement/approval/${data}`);
    },
    // 读取当前页缓存
    getLocalPageNo() {
      let localData = getSessionItem("pageNo", this.tableList.orderApproval);
      return (localData && localData.pageNo) || 1;
    },
    // 获取默认分页
    getLocalPageSize() {
      return getSessionItem("pageSize", this.tableList.orderApproval) || 10;
    },
    // 单元格点击跳转详情
    cellClick(r, column) {
      if (column.label === "订单编号") {
        // S0201录入
        if(r.todoData.businessBillTypeCode == 'S0201'){
          this.$router.push({
            path:`/orderManagement/entry/detail/${r.businessData.id}`,
            query:{
              type:'sunyao'
            }
          })
        }else{
          this.$router.push({
            path:`/orderManagement/close/approving-detail/${r.businessData.id}`,
            query:{
              type:'sunyao'
            }
          })
        }
        
      }
    },
    cellStyle({ row, column, rowIndex, columnIndex }) {
      if (column.label == "订单编号") {
        return { color: "#0089EF", cursor: "pointer" };
      }
    }
  },
  // beforeRouteLeave(to, from, next) {
  //   for (let key in this.$vnode.parent.componentInstance.cache) {
  //     delete this.$vnode.parent.componentInstance.cache[
  //       ishasAgentId(to.path, key)
  //     ];
  //   }
  //   next();
  // }
};
